# # from selenium import webdriver
# # from selenium.webdriver.chrome.service import Service
# # from selenium.webdriver.common.by import By
# # from selenium.webdriver.common.action_chains import ActionChains
# # import time
# # import pandas  as pd
# # class Grap:  # 类名应该以大写字母开头
# #     def __init__(self):
# #         # 设置驱动，selenium的新用法
# #         self.service = Service(r"C:\Program Files\Google\Chrome\Application\chromedriver.exe") 
# #             #实例化浏览器对象
# #         self.driver = webdriver.Chrome(service=self.service)
# #         self.df = pd.DataFrame(columns=("",""))
# #     def get_nowlaying(self):
# #         self.driver.get("https://www.douban.com/cinema/nowlaying/leshan/")
# #         time.sleep(2)
# #         elenments =self.driver.find_element(By.XPATH,"//ul[@class = 'list']/li[@class='list-item']")
# #         for ele in elenments:
# #             did =ele.get_attribute("id")
# #             didtitle =ele.get_attribute("data-title")
# #             dscore=ele.get_attributu("data-score")
# #             dstar=ele.get_attributu("data-star")
# #             dyear=ele.get_attributu("data-year")
# #             ddura=ele.get_attributu("data-duration")
# #             ddirector=ele.get_attributu("data-director")
# #             dactors=ele.get_attributu("data-actors")
# #             dvote =ele.get_attributu("data-votecount")


# #             self.df.loc[i] = [did, didtitle, dscore, dstar, dyear, ddura, ddirector, dactors, dvote]
# #             i += 1

# #         self.df.to_csv(self.df_save_path, index=None)

# #     def close(self):
# #         self.driver.quit()


# # # 使用示例
# # if __name__ == "__main__":
# #     grap = Grap()
# #     grap.get_nowplaying()
# #     grap.close()



# # if __name__ == "__main__":  # 注意这里的空格
# #     g = Grap()  # 类名应为 Grap
# #     g.drag_and_drop_test()  # 调用方法

            
            

# from selenium import webdriver
# from selenium.webdriver.chrome.service import Service
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.action_chains import ActionChains
# import time
# import pandas  as pd
# class Grap:  # 类名应该以大写字母开头
#     def __init__(self):
#         # 设置驱动，selenium的新用法
#         self.service = Service(r"C:\Program Files\Google\Chrome\Application\chromedriver.exe") 
#             #实例化浏览器对象
#         self.driver = webdriver.Chrome(service=self.service)
#         self.df = pd.DataFrame(
#             columns=["id", "title", "score", "star", "year", "duration", "director", "actors", "vote"])

#         self.df_save_path = "./data/nowplay.csv"
#         self.sc_df = pd.DataFrameta(columns=["rating","comment","score","star","year","duration",\])
#         self.sc_save_path ="./data/nowplay.csv"
#         # 检查并创建目录
#         os.makedirs(os.path.dirname(self.df_save_path), exist_ok=True)

#     def get_nowplaying(self):
#         self.driver.get("https://movie.douban.com/cinema/nowplaying/leshan/")
#         time.sleep(2)

#         elements = self.driver.find_elements(By.XPATH, "//ul[@class='lists']/li[@class='list-item']")

#         # 初始化计数器
#         i = 0

#         for ele in elements:
#             did = ele.get_attribute("id")
#             didtitle = ele.get_attribute("data-title")
#             dscore = ele.get_attribute("data-score")
#             dstar = ele.get_attribute("data-star")
#             dyear = ele.get_attribute("data-year")
#             ddura = ele.get_attribute("data-duration")
#             ddirector = ele.get_attribute("data-director")
#             dactors = ele.get_attribute("data-actors")
#             dvote = ele.get_attribute("data-votecount")

#             self.df.loc[i] = [did, didtitle, dscore, dstar, dyear, ddura, ddirector, dactors, dvote]
#             i += 1

#         self.df.to_csv(self.df_save_path, index=None)

#     def get_short_comment(self):
#         self.driver.get("https://movie.douban.com/cinema/nowplaying/leshan/")
#         elements1= self.driver.find_elements(By.T)


#     def close(self):
#         self.driver.quit()


# # 使用示例
# if __name__ == "__main__":
#     grap = Grap()
#     grap.get_nowplaying()
#     grap.close()


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
import pandas as pd
import os

class Grap:  # 类名应该以大写字母开头
    def __init__(self):
        # 设置驱动，selenium的新用法
        self.service = Service(r"C:\Program Files\Google\Chrome\Application\chromedriver.exe") 
        # 实例化浏览器对象
        self.driver = webdriver.Chrome(service=self.service)
        self.df = pd.DataFrame(
            columns=["id", "title", "score", "star", "year", "duration", "director", "actors", "vote"])

        self.df_save_path = "./data/nowplay.csv"
        self.sc_df = pd.DataFrame(columns=["rating","comment","score","star","year","duration"])
        self.sc_save_path = "./data/short_comments.csv"
        # 检查并创建目录
        os.makedirs(os.path.dirname(self.df_save_path), exist_ok=True)
        os.makedirs(os.path.dirname(self.sc_save_path), exist_ok=True)

    def get_nowplaying(self):
        self.driver.get("https://movie.douban.com/cinema/nowplaying/leshan/")
        time.sleep(2)

        elements = self.driver.find_elements(By.XPATH, "//ul[@class='lists']/li[@class='list-item']")

        # 初始化计数器
        i = 0

        for ele in elements:
            did = ele.get_attribute("id")
            didtitle = ele.get_attribute("data-title")
            dscore = ele.get_attribute("data-score")
            dstar = ele.get_attribute("data-star")
            dyear = ele.get_attribute("data-year")
            ddura = ele.get_attribute("data-duration")
            ddirector = ele.get_attribute("data-director")
            dactors = ele.get_attribute("data-actors")
            dvote = ele.get_attribute("data-votecount")

            self.df.loc[i] = [did, didtitle, dscore, dstar, dyear, ddura, ddirector, dactors, dvote]
            i += 1

        self.df.to_csv(self.df_save_path, index=None)

    def get_short_comment(self, min_rating=5):
        self.driver.get("https://movie.douban.com/cinema/nowplaying/leshan/")
        time.sleep(2)

        elements = self.driver.find_elements(By.XPATH, "//div[@class='comment-item']")

        # 初始化计数器
        j = 0

        for ele in elements:
            rating = int(ele.find_element(By.XPATH, ".//span[@class='rating']").get_attribute("class").split()[-1])
            comment = ele.find_element(By.XPATH, ".//span[@class='short']").text
            score = ele.find_element(By.XPATH, ".//span[@class='rating']").get_attribute("title")
            star = ele.find_element(By.XPATH, ".//span[@class='rating']").get_attribute("class").split()[-1]
            year = ele.find_element(By.XPATH, ".//span[@class='year']").text
            duration = ele.find_element(By.XPATH, ".//span[@class='duration']").text
            director = ele.find_element(By.XPATH, ".//span[@class='director']").text
            actors = ele.find_element(By.XPATH, ".//span[@class='actors']").text
            vote = ele.find_element(By.XPATH, ".//span[@class='votes']").text

            if rating >= min_rating:
                self.sc_df.loc[j] = [rating, comment, score, star, year, duration]
                j += 1

        self.sc_df.to_csv(self.sc_save_path, index=None)

    def close(self):
        self.driver.quit()

# 使用示例
if __name__ == "__main__":
    grap = Grap()
    grap.get_nowplaying()
    grap.get_short_comment(min_rating=8)  # 筛选评分大于等于8的评论
    grap.close()
